﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFMINERP.MODEL;
using WCFMINERP.DAL;
using WCFMINERP.COM;

namespace WCFMINERP.BLL.Console
{
    public class ConsoleBll
    {
        public List<UploadQueue_Info> GetLoadList(DateTime inStartTime, DateTime inEndTime)
        {
            List<UploadQueue_Info> rtnList = new List<UploadQueue_Info>();


            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                List<UploadQueue> listQ = db.UploadQueue.Where(p => p.CreateTime > inStartTime && p.CreateTime < inEndTime).ToList();
                foreach (UploadQueue uq in listQ)
                {
                    uq.ShopReference.Load();
                    UploadQueue_Info info = new UploadQueue_Info()
                    {
                        CreateTime = uq.CreateTime,
                        FileLength = uq.FileLength,
                        Remark = uq.Remark,
                        ShopId = uq.Shop.ShopId,
                        ShopName = uq.ShopName,
                        UploadId = uq.UploadId,
                        ZipName = uq.ZipName
                    };
                    rtnList.Add(info);
                }
            }
            return rtnList;

        }
        public List<UploadQueue_Info> GetErrorLoadList()
        {
            List<UploadQueue_Info> rtnList = new List<UploadQueue_Info>();


            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int errorStatus = (int)TransferStatus.Error;
                List<UploadQueue> listQ = db.UploadQueue.Where(p => p.Status == errorStatus).ToList();
                foreach (UploadQueue uq in listQ)
                {
                    uq.ShopReference.Load();
                    UploadQueue_Info info = new UploadQueue_Info()
                    {
                        CreateTime = uq.CreateTime,
                        FileLength = uq.FileLength,
                        Remark = uq.Remark,
                        ShopId = uq.Shop.ShopId,
                        ShopName = uq.ShopName,
                        UploadId = uq.UploadId,
                        ZipName = uq.ZipName
                    };
                    rtnList.Add(info);
                }
            }
            return rtnList;

        }
        /// <summary>
        /// 重新解析下载包
        /// </summary>
        /// <param name="inBagName"></param>
        /// <returns></returns>
        public int CopyBack(string inBagName)
        {
            int rtnVal = 0;
            string uploadPath = "";
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    uploadPath = db.FileDirPath.FirstOrDefault(p => p.Des == "下载目录").FileDir;

                    string zipBackFolder = uploadPath.Substring(0, uploadPath.LastIndexOf("\\")) + "ZipBack";
                    string zipBackFilePath = zipBackFolder + "\\" + inBagName + ".zip";
                    string zipToFilePath = uploadPath + "\\" + inBagName;
                    if (FileOperate.CopyFile(zipBackFilePath, zipToFilePath, true))
                    {
                        UploadQueue uq = db.UploadQueue.FirstOrDefault(p => p.ZipName == inBagName);
                        if (uq != null)
                        {
                            uq.Status = (int)TransferStatus.Done;
                            db.SaveChanges();
                            rtnVal = 1;
                        }
                        else
                        {
                            rtnVal = -1;///数据未找到
                        }
                    }
                    else
                    {
                        rtnVal = -2;
                        //拷贝回文件未成功
                    }


                }
            }
            catch (Exception exx)
            {
                rtnVal =- 100;
            }
            return rtnVal;

        }

        //public int ReLoad(string inBagName)
        //{
        //    int rtnVal = 0;
        //    using (newSampleYPDBEntities db = new newSampleYPDBEntities())
        //    {
        //        UploadQueue uq = db.UploadQueue.FirstOrDefault(p => p.ZipName == inBagName);
        //        if (uq != null)
        //        {
        //            uq.Status = (int)TransferStatus.Package;
        //            db.SaveChanges();
        //            rtnVal = 1;
        //        }
        //    }
        //}
    }
}
